package com.management.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.management.commons.exception.CreateRecordException;
import com.management.commons.exception.DeleteRecordByIdException;
import com.management.commons.exception.ExceptionCode.DefectRecordExceptionCode;
import com.management.commons.exception.GetAllRecordsException;
import com.management.commons.exception.changeassigneeException;
import com.management.controller.form.*;
import com.management.controller.info.*;
import com.management.dao.*;
import com.management.model.*;
import com.management.service.DefectRecordService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.management.utils.CopyUtils;
import com.management.utils.Result;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 龙世超
 * @since 2022-03-23
 */
@Service
@Transactional
public class DefectRecordServiceImpl extends ServiceImpl<DefectRecordDao, DefectRecord> implements DefectRecordService {

    @Resource
    private ProjectInfoDao projectInfoDao;

    @Resource
    private AttachDao attachDao;

    @Resource
    private AccountDao accountDao;

    @Resource
    private AccountRoleProjectDao accountRoleProjectDao;


    @Override
    public Result FindAllProject() {
        List<ProjectInfo> projectInfos = projectInfoDao.selectList(null);
        List list = CopyUtils.CopyList(projectInfos, GetAllProjectInfo.class);
        return Result.getInstance().setData(list);
    }

    @Override
    public Result FindAllDefect() {
        List<DefectRecord> defectRecords = baseMapper.selectList(null);
        List list = CopyUtils.CopyList(defectRecords, GetAllDefectInfo.class);
        return Result.getInstance().setData(list);
    }

    @Override
    public Result AddDefectProject(CreateDefectProjectForm defectProject) {
        DefectRecord defectRecord = CopyUtils.CopyOne(defectProject, DefectRecord.class);
        AccountInfo principal = (AccountInfo) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        defectRecord.setCreateBy(principal.getAccountName());
//        defectRecord.setCreateBy("longshichao");
        defectRecord.setIssueState("新建");
        int i = baseMapper.insert(defectRecord);
        if (i > 0) {
            Integer defectId = defectRecord.getDefectId();
            Attach attach = attachDao.selectById(defectProject.getAttachId());
            attach.setDefectId(defectId);
            attachDao.updateById(attach);

            return Result.getInstance().setMessage("新增成功");
        }
        throw new CreateRecordException(DefectRecordExceptionCode.CREATERECORD_ERROR);
    }

    @Override
    public Result FindAllDefectRecords(GetRecordsPageForm getRecordsPageForm) {

        AccountInfo accountInfo = (AccountInfo)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        getRecordsPageForm.setAccountId(accountInfo.getAccountId());
        GetRecordsPageForm recordsPage = baseMapper.selectDefectRecords(getRecordsPageForm);

        if (recordsPage != null) {
            Page<DefectRecord> page = new Page<>();
            List list = CopyUtils.CopyList(recordsPage.getRecords(), GetAllRecoedsInfo.class);
            page.setTotal(recordsPage.getTotal());
            page.setPages(recordsPage.getPages());
            page.setCurrent(recordsPage.getCurrent());
            page.setSize(recordsPage.getSize());
            page.setRecords(list);
            return Result.getInstance().setData(page);
        }
        throw new GetAllRecordsException(DefectRecordExceptionCode.GETALLRECORDS_ERROR);
    }

    @Override
    public Result FindAllDefectProject() {

        List<DefectRecord> defectRecords = baseMapper.selectList(null);
        List list = CopyUtils.CopyList(defectRecords, GetAllDefectProjectForm.class);
        return Result.getInstance().setData(list);
    }

    @Override
    public Result FindDefectProjectById(GetDefectProjectByIdForm projectByIdForm) {
        DefectRecord defectRecord = baseMapper.selectById(projectByIdForm.getDefectId());
        Attach attach=attachDao.selectBydefectId(projectByIdForm.getDefectId());


        GetRecoedByIdInfo recoedByIdInfo = CopyUtils.CopyOne(defectRecord, GetRecoedByIdInfo.class);
        recoedByIdInfo.setAttachOldname(attach.getAttachOldname());
        recoedByIdInfo.setAttachDesc(attach.getAttachDesc());

        if (defectRecord.getParentDefectId() != 0) {
            DefectRecord defectRecord1 = baseMapper.selectById(defectRecord.getParentDefectId());
            recoedByIdInfo.setParentDefectName(defectRecord1.getDefectName());
        }
        return Result.getInstance().setData(recoedByIdInfo);
    }

    @Override
    public Result DeleteRecordById(GetDefectProjectByIdForm projectByIdForm) {
        int i = baseMapper.deleteById(projectByIdForm.getDefectId());
        if (i > 0) {
            return Result.getInstance().setMessage("删除成功");
        }
        throw new DeleteRecordByIdException(DefectRecordExceptionCode.DELETERECORD_ERROR);
    }

    @Override
    public Result editById(GetDefectProjectByIdForm projectByIdForm) {
        DefectRecord defectRecord = baseMapper.selectById(projectByIdForm.getDefectId());
        EditByIdInfo editById = CopyUtils.CopyOne(defectRecord, EditByIdInfo.class);
        return Result.getInstance().setData(editById);
    }

    @Override
    public Result changeRecord(EditByIdForm editByIdForm) {
        DefectRecord defectRecord = CopyUtils.CopyOne(editByIdForm, DefectRecord.class);
        if (defectRecord.getParentDefectId()==null) {
            defectRecord.setParentDefectId(0);
        }
        baseMapper.updateById(defectRecord);
        if (editByIdForm.getAttachId() != null) {
            Integer integer = attachDao.deleteByDefectId(defectRecord.getDefectId());
            if (integer > 0) {
                Attach attach = attachDao.selectById(editByIdForm.getAttachId());
                attach.setDefectId(editByIdForm.getDefectId());
                attachDao.updateById(attach);
            }
        }
        return Result.getInstance().setMessage("修改成功");

    }

    @Override
    public Result Findassignee(String projectName) {
        QueryWrapper<ProjectInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("project_name",projectName);
        ProjectInfo projectInfo = projectInfoDao.selectOne(queryWrapper);
        QueryWrapper<AccountRoleProject> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("project_id",projectInfo.getProjectId());
        List<AccountRoleProject> accountRoleProjects = accountRoleProjectDao.selectList(queryWrapper1);
        ArrayList<Account> accounts = new ArrayList<>();
        for (AccountRoleProject project : accountRoleProjects) {
            Account account = accountDao.selectOne(new QueryWrapper<Account>().eq("account_id", project.getAccountId()));
            accounts.add(account);
        }

        List list = CopyUtils.CopyList(accounts, GetassigneeInfo.class);
        return Result.getInstance().setData(list);
    }

    @Override
    public Result changassignee(changeassignForm changeassignForm) {
        DefectRecord defectRecord = baseMapper.selectById(changeassignForm.getDefectId());
        defectRecord.setAssignee(changeassignForm.getAssignee());
        defectRecord.setAssignedTime(LocalDateTime.now());
        int i = baseMapper.updateById(defectRecord);
        if (i>0) {
            return Result.getInstance().setMessage("修改成功");
        }
        throw new changeassigneeException(DefectRecordExceptionCode.CHANGEASSIGNEE_ERROR);
    }

    @Override
    public Result FindRecordById(GetDefectProjectByIdForm projectByIdForm) {
        DefectRecord defectRecord = baseMapper.selectById(projectByIdForm.getDefectId());
        HandleRecordByIdInfo recordByIdInfo = CopyUtils.CopyOne(defectRecord, HandleRecordByIdInfo.class);
        return Result.getInstance().setData(recordByIdInfo);
    }

    @Override
    public Result SaveHandle(SaveHandleForm saveHandleForm) {
        DefectRecord record = CopyUtils.CopyOne(saveHandleForm, DefectRecord.class);
        baseMapper.updateById(record);
        return Result.getInstance().setMessage("处理完成");
    }

    @Override
    public Result getStateCount(GetStateCountForm getStateCountForm) {
       List<AllStateCountForm> list= baseMapper.getStateCount(getStateCountForm);
        return Result.getInstanceList(AllStateCountForm.class).setData(list);
    }

    @Override
    public Result getAllTypeCount() {
       List<GetTypeCountInfo> list= baseMapper.getAllTypeCount();
        return Result.getInstanceList(GetTypeCountInfo.class).setData(list);
    }

    @Override
    public Result getTypeCount(GetStateCountForm getStateCountForm) {
       List<GetTypeCountInfo> list=baseMapper.getTypeCount(getStateCountForm);
        return Result.getInstanceList(GetTypeCountInfo.class).setData(list);
    }

    @Override
    public Result getAllSeverityCount() {
        List<GetSeverityCountInfo> list=baseMapper.getAllSeverityCount();
        return Result.getInstanceList(GetSeverityCountInfo.class).setData(list);
    }

    @Override
    public Result getSeverityCount(GetSeverityCountForm getSeverityCountForm) {
       List<GetSeverityCountInfo> list= baseMapper.getSeverityCount(getSeverityCountForm);
        return Result.getInstanceList(GetSeverityCountInfo.class).setData(list);
    }

    @Override
    public Result deleteRecords(deleteRecordsForm deleteRecordsForm) {
        int i=0;
        for (Integer integer : deleteRecordsForm.getList()) {
            i += baseMapper.deleteById(integer);
        }
        if (i>0){
            return Result.getInstance().setMessage("删除成功");
        }
        throw new DeleteRecordByIdException(DefectRecordExceptionCode.DELETERECORD_ERROR);
    }

    @Override
    public Result FindDefectRecordsByaccountId(GetRecordsPageForm getRecordsPageForm) {
        AccountInfo accountInfo = (AccountInfo)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        QueryWrapper<DefectRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("assignee",accountInfo.getAccountName());
        GetRecordsPageForm recordsPage = baseMapper.selectPage(getRecordsPageForm, queryWrapper);
        if (recordsPage != null) {
            Page<DefectRecord> page = new Page<>();
            List list = CopyUtils.CopyList(recordsPage.getRecords(), GetAllRecoedsInfo.class);
            page.setTotal(recordsPage.getTotal());
            page.setPages(recordsPage.getPages());
            page.setCurrent(recordsPage.getCurrent());
            page.setSize(recordsPage.getSize());
            page.setRecords(list);
            return Result.getInstance().setData(page);
        }
        throw new GetAllRecordsException(DefectRecordExceptionCode.GETALLRECORDS_ERROR);
    }
}
